package org.xiaozhiying.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.xiaozhiying.entity.CityWeatherEntity;

public class JsonUtil {
    public static CityWeatherEntity parseJsonToObject(String jsonStr) {
        if (jsonStr != null) {
            JSONObject jsonObject = JSON.parseObject(jsonStr);
            if (jsonObject != null) {
                JSONArray jsonArray = jsonObject.getJSONArray("lives");
                if(jsonArray != null && jsonArray.size() > 0) {
                    JSONObject weatherObject = jsonArray.getJSONObject(0);
                    CityWeatherEntity weatherEntity = JSON.parseObject(weatherObject.toJSONString(), CityWeatherEntity.class);
                    return weatherEntity;
                }
            }
        }
        return null;
    }
}
